var _visitor_index_js = {
    template:'{{#  layui.each(d, function(index, item){ }}\n' +
    '    <div class="blogs">\n' +
    '        <figure><img src="{{ item.image }}"></figure>\n' +
    '        <ul>\n' +
    '            <h3><a class="visitor-article" href="/visitor/article.action?id={{ item.id }}">{{ item.title }}</a></h3>\n' +
    '            <div>{{ item.text }}</div>\n' +
    '            <p class="autor">\n' +
    '                <span class="lm f_l">{{ item.source }}</span>\n' +
    '                <span class="dtime f_l">{{ item.postdate }}</span>\n' +
    '                <span class="viewnum f_r">（{{ item.browsecount }}）</span>\n' +
    '                <span class="pingl f_r">（{{ item.endorsecount }}）</span>\n' +
    '            </p>\n' +
    '        </ul>\n' +
    '    </div>\n' +
    '    {{#  }); }}',

    index:'<div class="layui-carousel" id="index_carousel" style="margin: auto;">\n' +
    '    <div id="carousel_item" carousel-item style="width: 60%;margin: auto;">\n' +
    '    </div>\n' +
    '</div>\n' +
    '\n' +
    '<!-- banner代码 结束 -->\n' +
    '<div class="topnews">\n' +
    '    <h2><b>文章</b>推荐</h2>\n' +
    '    <div id="index_article"></div>\n' +
    '</div>',

    laytpl: null,
    carousel: null,
    init: function () {
        $('#main_content_div').html(_visitor_index_js.index);
        _visitor_index_js.carousel_init();
        _visitor_index_js.article_init();
    },
    carousel_init: function () {
        /*$.ajax({
            url: '/article/getOrderRank.action',
            type: 'post',
            async: false,
            success: function (data) {

            }
        });*/
        $.post('/article/getOrderRank.action',function (data) {
            var template = '{{#  layui.each(d, function(index, item){ }}' +
                '<a href="/visitor/article.action?id={{ item.id }}"><img src="{{ item.image }}" style="width: 100%;height: 100%;"/></a>' +
                '{{#  }); }}';
            _visitor_index_js.laytpl(template).render(data.data, function(html){
                $('#carousel_item').html(html);
            });
            _visitor_index_js.carousel.render({
                elem: '#index_carousel'
                ,width: '100%' //设置容器宽度
                ,arrow: 'always' //始终显示箭头
                ,anim: 'fade' //切换动画方式
                ,indicator: 'outside'
            });
        });
    },
    article_init: function () {
        var _par = window.location.search;
        $.post('/article/getByType.action'+_par,function (data) {
            var value = data.data;
            if (value != null){
                for (var i = 0; i < value.length; i++){
                    value[i].text = value[i].text.replace(/<[^>]+>/g,"");//去掉所有的html标记
                    value[i].text = value[i].text.length > 110 ? value[i].text.substring(0,100) + '......':value[i].text;
                    var date = new Date(parseInt(value[i].postdate));
                    //date.setTime(parseInt(value[i].postdate));
                    value[i].postdate = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();//yyyy-MM-dd格式日期
                }
            }
            _visitor_index_js.laytpl(_visitor_index_js.template).render(value, function(html){
                $('#index_article').html(html);
            });
        });
    }
}